package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Subbranch;
import com.gxa.zhonglong.service.HeadquartersService;
import com.gxa.zhonglong.service.SubbranchService;
import com.gxa.zhonglong.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/25 14:22
 * Description:分部操作接口
 * Version: V1.0
 */
@Api(tags ="分店接口api")
@CrossOrigin
@RestController
@RequestMapping("/subbranch")
public class SubbranchController {

    @Autowired
    private SubbranchService subbranchService;
    @Autowired
    private HeadquartersService headquartersService;

    @PostMapping("/selectAllById")
    @ApiOperation("根据总部id查询旗下所有分部信息")
    public Map<String,Object> selectAllById(Long id) throws BusinessException {
        final List<Map<String, Object>> maps = subbranchService.selectAllById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectById")
    @ApiOperation("根据分店id查询信息")
    public Map<String,Object> selectById(Long id) throws BusinessException {
        final Map<String, Object> maps = subbranchService.selectById(id);
        final Map<String, Object> hid = headquartersService.selectById((Long) maps.get("hid"));
        List<Map<String,Object>> list=new ArrayList<>();
        list.add(hid);
        list.add(maps);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectIdAndName")
    @ApiOperation("根据总部id查询旗下所有分部信息(id和name)")
    public Map<String,Object> selectIdAndName(Long id) {
        final List<Map<String, Object>> maps = subbranchService.selectIdAndName(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectAll")
    @ApiOperation("查询所有分部信息(id和name)")
    public Map<String,Object> selectAll() {
        final List<Map<String, Object>> maps = subbranchService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改分部")
    public Map<String,Object> addRoUpdate(Subbranch subbranch) throws BusinessException {
        JsonResult jsonResult = new JsonResult();

        if (subbranch.getId()==null){
            if(!StringUtil.isPhoneNumber(subbranch.getPhone())){
                throw new BusinessException(ExceptionCode.BAD_PHONE);
            }
            subbranchService.add(subbranch);
            jsonResult.setMsg("添加成功");
        }else {
            if(!StringUtil.isEmpty(subbranch.getPhone())){
                if(!StringUtil.isPhoneNumber(subbranch.getPhone())){
                    throw new BusinessException(ExceptionCode.BAD_PHONE);
                }
            }
            subbranchService.update(subbranch);
            jsonResult.setMsg("修改成功");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/del")
    @ApiOperation("根据id删除分部")
    public Map<String,Object> del(Long id) throws BusinessException {
        subbranchService.del(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
